import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:jiffy/jiffy.dart';
import 'package:safetymonitor/app/components/custom_app_bar.dart';
import 'package:safetymonitor/app/components/custom_modele/custom_intrinsic_circle_line.dart';
import 'package:safetymonitor/app/components/custom_modele/custom_title_chart_page.dart';
import 'package:safetymonitor/app/core/core.dart';
import 'package:safetymonitor/app/core/ext/ext.dart';
import 'package:safetymonitor/app/data/entities/application/get_realtime_person_monitoring_entity.dart';

import 'detail_controller.dart';

/// 人员详情页面
class DetailPage extends StatelessWidget {
  DetailPage({super.key});

  final controller = Get.put(DetailController());

  @override
  Widget build(BuildContext context) {
    return GetBuilder(
      init: controller,
      builder: (_) {
        return Scaffold(
          backgroundColor: const Color(0xFFF7FBFE),
          appBar: CustomAppBar(
           titleText: '人员详情',
          ),
          body: Stack(
            children: [
              Container(
                alignment: Alignment.center,
                height: 102.w,
                color: const Color(0xFFF7FBFE),
              ),
              Container(
                padding: EdgeInsets.symmetric(horizontal: 16.w),
                child: SingleChildScrollView(
                  child: Column(
                    children: [
                      AppGaps.vGap16,
                      CustomTitleChartPage(
                        contentPadding:
                            EdgeInsets.only(right: 16.w, left: 16.w, top: 16.w),
                        title: controller.entity.companyName,
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            Row(
                              children: [
                                Expanded(
                                    child: _getTextView(
                                        title: '人员姓名:',
                                        value: controller.entity.name)),
                                Expanded(
                                    child: _getTextView(
                                        title: '性别:',
                                        value: controller.entity.sex == 1
                                            ? '男'
                                            : '女')),
                              ],
                            ),
                            Row(
                              children: [
                                Expanded(
                                    child: _getTextView(
                                        title: '电话:',
                                        value: controller.entity.phone)),
                                Expanded(
                                    child: _getTextView(
                                        title: '身份证号码:',
                                        value: controller.entity.idCard)),
                              ],
                            ),
                            Row(
                              children: [
                                Expanded(
                                    child: _getTextView(
                                        title: '人员分组:',
                                        value: controller.entity.groupId)),
                                Expanded(
                                    child: _getTextView(
                                        title: '人员工种:',
                                        value: controller
                                                .pageEntityEntity.craftStr ??
                                            '')),
                              ],
                            ),
                            _getTextView(
                              title: '报警开始:',
                              value: controller.entity.enterTime.isNotEmpty
                                  ? Jiffy.parseFromMillisecondsSinceEpoch(
                                          int.parse(
                                              controller.entity.enterTime))
                                      .format(pattern: 'yyyy-MM-dd HH:mm:ss')
                                  : '',
                            ),
                            _getTextView(
                              title: '报警结束:',
                              value: controller.entity.exitTime.isNotEmpty
                                  ? Jiffy.parseFromMillisecondsSinceEpoch(
                                          int.parse(
                                              controller.entity.exitTime ??
                                                  '0'))
                                      .format(pattern: 'yyyy-MM-dd HH:mm:ss')
                                  : '',
                            ),
                            AppGaps.vGap6,
                          ],
                        ),
                      ),
                      AppGaps.vGap16,
                      controller.entity.personLocationDataDO.isNotEmpty
                          ? _getDisposalInfo()
                          : Center(
                              child: Column(
                                children: [
                                  AppGaps.vGap30,
                                  SvgPicture.asset(
                                      'assets/svg/zhiban_empty_bg.svg'),
                                  Text(
                                    '暂无人员轨迹~',
                                    style: TextStyle(
                                      color: const Color(0xFFC7CEE5),
                                      fontSize: 14.sp,
                                    ),
                                  ),
                                ],
                              ),
                            ),
                      AppGaps.vGap60,
                    ],
                  ),
                ),
              ),
            ],
          ),
        );
      },
    );
  }

  Widget _getDisposalInfo() {
    return ListView.builder(
        padding: EdgeInsets.symmetric(horizontal: 27.w, vertical: 22.w),
        itemCount: controller.entity.personLocationDataDO.length,
        shrinkWrap: true,
        physics: const BouncingScrollPhysics(),
        itemBuilder: (context, index) {
          return CustomIntrinsicCircleLine(
            iconWidget: _getIconView(index),
            showWidget: _getTrainView(
                controller.entity.personLocationDataDO[index], index),
          );
        });
  }

  /// 获取字段显示
  Widget _getTextView({required String title, required String value}) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Row(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(
              title,
              style: TextStyle(
                fontSize: 14.sp,
                height: 1.2,
                fontWeight: FontWeight.w400,
                color: const Color(0xffC7CEE5),
              ),
            ),
            AppGaps.hGap6,
            Expanded(
                child: Text(
              value,
              style: TextStyle(
                fontSize: 14.sp,
                height: 1.2,
                fontWeight: FontWeight.w400,
                color: const Color(0xff1D3452),
              ),
            ))
          ],
        ),
        AppGaps.vGap10,
      ],
    );
  }

  ///获取历史显示
  Widget _getTrainView(
      GetRealtimePersonMonitoringPersonLocationDataDO personLocationDataDO,
      int index) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Row(
          children: [
            Expanded(
              child: Text(
                personLocationDataDO.locationUpdateTime.millTS2Str(),
                style: TextStyle(
                  fontSize: 15.sp,
                  fontWeight: FontWeight.w400,
                  color: const Color(0xff262F38),
                ),
              ),
            ),
          ],
        ),
        AppGaps.vGap6,
        Container(
          padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 5.w),
          decoration: BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.only(
              topRight: Radius.circular(10.r),
              topLeft: Radius.circular(10.r),
              bottomRight: Radius.circular(10.r),
              bottomLeft: Radius.circular(10.r),
            ),
            boxShadow: [
              BoxShadow(
                color: const Color(0xffBCBCC5).withOpacity(0.3), // 阴影颜色
                offset: const Offset(0, -5), // 阴影的偏移量，向上偏移，即上方阴影
                blurRadius: 10.0, // 阴影的模糊半径
                spreadRadius: 2.0, // 阴影的扩散半径
              ),
            ],
          ),
          child: Column(
            children: [
              AppGaps.vGap10,
              _getTextView(
                  title: '站点名称：', value: personLocationDataDO.stationName),
              _getTextView(
                  title: '区域位置：',
                  value: personLocationDataDO.personTrackDO.locationAerial),
            ],
          ),
        ),
        AppGaps.vGap10,
      ],
    ).clickOnce(() {
      //传入数据 显示人员轨迹
      controller.onClick(personLocationDataDO);
    });
  }

  /// 获取图标样式
  Widget _getIconView(int index) {
    return Container(
      alignment: Alignment.center,
      decoration: BoxDecoration(
        color: const Color(0xff5C6DFF),
        borderRadius: BorderRadius.circular(8.r),
      ),
      width: 28.w,
      height: 28.w,
      child: Text(
        '${controller.entity.personLocationDataDO.length - index}',
        style: TextStyle(
          fontSize: 14.sp,
          height: 1.2,
          fontWeight: FontWeight.w400,
          color: const Color(0xffffffff),
        ),
      ),
    );
  }
}
